package com.grm.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.grm.common.Result;
import com.grm.sys.entity.SysLog;
import com.grm.sys.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys/log")
public class SysLogController {
    @Autowired
    private SysLogService sysLogService;

    @GetMapping("/query/selective")
    public Result querySelective(@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        //使用分页插件
        PageHelper.startPage(pageNum, pageSize);
        // 查询条件
        QueryWrapper<SysLog> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        List<SysLog> sysLogList = sysLogService.list(wrapper);
        //获取总数
        PageInfo<SysLog> pageInfo = new PageInfo<>(sysLogList);
        Long total = pageInfo.getTotal();
        return Result.success("查询操作日志列表成功！",sysLogList,total);
    }

    @PreAuthorize("hasAuthority('sys:log:clearAll')")
    @DeleteMapping("/clearAll")
    public Result clearAllSysLog(){
        sysLogService.remove(null);
        return Result.success("清除系统日志成功！");
    }
}
